package com.newsnetwork.admin.controller;

import com.newsnetwork.admin.service.FriendLinkService;
import com.newsnetwork.api.BaseController;
import com.newsnetwork.api.controller.admin.FriendLinkControllerApi;
import com.newsnetwork.grace.result.CommonJSONResult;
import com.newsnetwork.pojo.bo.SaveFriendLinkBO;
import com.newsnetwork.pojo.mo.FriendLinkMO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: HuangDekai
 * @date: 2022/11/10 15:10
 * @version: 1.0
 * @since: jdk8
 * @description: 添加友情链接
 */
@RestController
public class FriendLinkController extends BaseController implements FriendLinkControllerApi {

    private static final Logger log = LoggerFactory.getLogger(FriendLinkController.class);

    @Autowired
    private FriendLinkService friendLinkService;

    /**
     * 添加或更改友情链接
     * @param saveFriendLinkBO 友情链接bo
     * @param result 校验结果
     */
    @Override
    public CommonJSONResult saveOrUpdateFriendLink(@Valid SaveFriendLinkBO saveFriendLinkBO,
                                                   HttpServletRequest request,
                                                   HttpServletResponse response) {
//        if (result.hasErrors()) {
//            Map<String, String> map = getErrors(result);
//            return CommonJSONResult.errorMap(map);
//        }

        FriendLinkMO friendLinkMO = new FriendLinkMO();
        BeanUtils.copyProperties(saveFriendLinkBO, friendLinkMO);
        friendLinkMO.setCreateTime(new Date());
        friendLinkMO.setUpdatedTime(new Date());

        friendLinkService.saveOrUpdateFriendLink(friendLinkMO);

        return CommonJSONResult.ok();
    }

    @Override
    public CommonJSONResult getFriendLinkList() {
        return CommonJSONResult.ok(friendLinkService.queryAllFriendLinkList());
    }

    @Override
    public CommonJSONResult delete(String linkId) {
        friendLinkService.deleteFriendLinkById(linkId);
        return CommonJSONResult.ok();
    }


    /**
     * 首页查询友情链接
     * @return 同一封装的 友情链接列表
     */
    @Override
    public CommonJSONResult queryPortalAllFriendLinkList() {
        List<FriendLinkMO> friendLinkList = friendLinkService.queryPortalAllFriendLinkList();
        return CommonJSONResult.ok(friendLinkList);
    }
}
